package cn.akwangl.design.pattern.creational.prototype;

import org.apache.ibatis.cache.CacheKey;

import java.io.Serializable;
import java.util.Date;

/**
 * 用户模型.
 * <br/>
 * 克隆模式<br/>
 * <p>
 * 怎么实现?
 * 1. 实现{@link Cloneable} 接口<br/>
 * 2. 重写{@link Object#clone()} 方法<br/>
 * <br/>
 * 注意:深拷贝与浅拷贝.<br/>
 * 克隆模式的原理在于,创建对象时使用的不再是new关键字,而是通过拷贝对象二进制流来创建对象<br/>
 * 如果一个对象的属性有引用对象时,需要单独处理（深拷贝）,否则该对象的属性引用为同一个地址（浅拷贝）<br/>
 * </p>
 * <p>
 * 示例:
 * {@link CacheKey}
 *
 * @author youyongkun
 * @date 2021/11/24 14:29
 * @since 1.0
 */
public class User implements Serializable, Cloneable {

    private String username;

    private String paw;

    private String age;

    private Date createdDate;

    public User() {
    }

    @Override
    protected User clone() throws CloneNotSupportedException {
        User user = (User) super.clone();
        user.setCreatedDate((Date) getCreatedDate().clone());
        return user;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPaw() {
        return paw;
    }

    public void setPaw(String paw) {
        this.paw = paw;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public Date getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", paw='" + paw + '\'' +
                ", age='" + age + '\'' +
                ", createdDate=" + createdDate +
                '}';
    }
}
